package com.gjintel.business.proj.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.business.proj.dao.IProjectPauseDao;
import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.business.proj.model.ProjectPause;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.util.StringUtil;

@Repository("projectPauseDao")
public class ProjectPauseDaoImpl extends HibernateGenericDAO implements IProjectPauseDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectPause> getPrjectPause(String projectId, String category) {
		List<ProjectPause> list = new ArrayList<ProjectPause>();
		String hqlString = "from ProjectPause p where p.projectId=? and p.category=?";
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(projectId);
		paramList.add(category);
		list = (List<ProjectPause>) this.find(hqlString, paramList.toArray());
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectInfo> getProjectList(Page page, ProjectInfo project) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("select pi from ProjectInfo pi where 1=1 ");
		if (!StringUtil.isEmpty(project.getCode())) {
			hql.append("and pi.code like ? ");
			params.add("%" + project.getCode() + "%");
		}
		if (!StringUtil.isEmpty(project.getName())) {
			hql.append("and pi.name like ? ");
			params.add("%" + project.getName() + "%");
		}
		if (!StringUtil.isEmpty(project.getTenderType())) {
			hql.append("and pi.catagoryName = ? ");
			params.add(project.getTenderType());
		}
		hql.append("order by pi.createTime desc ");
		return this.findAsPage(hql.toString(), page, params.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectPause> getProjectPause(String category, String projectId) {
		String hql = "from ProjectPause p where p.category=? and p.projectId=?";
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(category);
		paramList.add(projectId);
		List<ProjectPause> list = (List<ProjectPause>) this.find(hql, paramList.toArray());
		return list;
	}

	@Override
	public List<ProjectPause>  getWorkingProjectPause(String category, String projectId) {
		//String queryString ="select max(p.pause_time) pauseTime,p.id id,p.restore_time restoreTime,p.proj_id projectId,p.requester_id requestId,p.auditor_id auditorId,p.reason reason,p.replay replay,p.category category,p.result result,p.flowReason flowReason,p.revokeReason revokeReason from t_proj_pause p where p.category=? and p.proj_id = ? ";
		 String queryString="select p.pause_time pauseTime,p.id id,p.restore_time restoreTime,p.proj_id projectId,p.requester_id requestId,p.auditor_id auditorId,p.reason reason,p.replay replay,p.category category,p.result result,p.flowReason flowReason,p.revokeReason revokeReason from t_proj_pause p where p.category=? and p.proj_id = ?  ORDER BY pause_time DESC LIMIT 0,1";
		 List<Object> paramList = new ArrayList<Object>();
		 paramList.add(category); 
		 paramList.add(projectId);
		 List<ProjectPause> list= this.findBySQL(ProjectPause.class, queryString,  paramList.toArray());
		return list;
	}

}
